﻿#if !(V1_1 || V2 || V3 || V3_5)
using System.Dynamic;
#endif

namespace V8.Net
{
    /// <summary>
    /// 句柄部分具有处理创建和处理托管句柄（包装本地V8句柄）的方法。
    /// 这有助于重用现有的句柄，以防止每次都必须创建新的句柄，从而大大加快了处理速度。
    /// </summary>
    public unsafe partial class V8Engine
    {
        // --------------------------------------------------------------------------------------------------------------------

        /// <summary>保存为此引擎实例创建的所有句柄代理的索引。</summary>
        internal HandleProxy*[] _HandleProxies = new HandleProxy*[1000];

        /// <summary>V8.NET系统中的句柄代理引用总数（供代理使用）。</summary>
        public int TotalHandles
        {
            get
            {
                lock (_HandleProxies)
                {
                    var c = 0;
                    foreach (var item in _HandleProxies)
                        if (item != null) c++;
                    return c;
                }
            }
        }
        /// <summary>V8.NET系统中准备由V8.NET垃圾收集器（工作线程）处理的句柄代理引用的总数。</summary>
        public int TotalHandlesPendingDisposal
        {
            get
            {
                lock (_HandleProxies)
                {
                    var c = 0;
                    foreach (var item in _HandleProxies)
                        if (item != null && item->IsDisposeReady) c++;
                    return c;
                }
            }
        }
        /// <summary>V8.NET系统中已缓存并准备重用的句柄总数。</summary>
        public int TotalHandlesCached
        {
            get
            {
                lock (_HandleProxies)
                {
                    var c = 0;
                    foreach (var item in _HandleProxies)
                        if (item != null && item->IsDisposed) c++;
                    return c;
                }
            }
        }
        void _Initialize_Handles()
        {
        }
    }
}
